package com.dcloud.file.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dcloud.file.ToolsApplication;
import com.dcloud.file.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName : LocalFileServiceImpl
 * @Description :
 * @Author : shijf
 * @Date: 2024-08-06 15:17
 */
@Service("localFileService")
@Slf4j
public class LocalFileServiceImpl implements FileService {

    private static String uploadFilePath = "";

    @Resource
    private ApplicationContext applicationContext;

    static {
        String osName = System.getProperty("os.name").toLowerCase();
        if (osName.contains("linux")) {
            uploadFilePath = "/home/sjf/uploadFile/";
        } else if (osName.contains("windows")) {
            uploadFilePath = "D://uploadFile/";
        } else {
            System.out.println("运行在其他操作系统上");
        }
    }


    @Override
    public Result upload(MultipartFile file) {
        try {

            String originalFilename = file.getOriginalFilename();
            String objectName =
                    ( originalFilename.indexOf(".")==-1
                    ?originalFilename + "_" + System.currentTimeMillis()
                    :originalFilename.substring(0, originalFilename.lastIndexOf(".")) + "_" + System.currentTimeMillis() + originalFilename.substring(originalFilename.lastIndexOf("."))
            );
            // 生成随机文件名
            log.info("uploadPath={}", uploadFilePath);
            File uploadDir = new File(uploadFilePath);
            if (!uploadDir.exists()) {
                uploadDir.mkdirs();
            }

            File localFile = new File(uploadFilePath + objectName);
            file.transferTo(localFile);



            Environment env = applicationContext.getEnvironment();
            InetAddress address = InetAddress.getLocalHost();
            String ip = address.getHostAddress();

            Map<String,String> res = new LinkedHashMap<>();
            res.put("storeName",objectName);
            res.put("fileName",originalFilename);
            res.put("fileUrl", "http://"+ip + ":" +  env.getProperty("server.port")+"/"+env.getProperty("server.servlet.context-path") + "/api/uploadfile/v1/download/" + objectName);
            res.put("filePath", uploadFilePath + objectName);

            return Result.ok(res);
        } catch (Exception e) {
            log.error("upload is error", e);
            return Result.error(e.getMessage());
        }
    }

    @Override
    public InputStream getFile(String objectName) throws FileNotFoundException {

        File file = new File(uploadFilePath+objectName);
        if (!file.exists()) {
          throw new RuntimeException("未找到该文件");
        }
        InputStream inputStream = new FileInputStream(uploadFilePath+objectName);

        return inputStream;
    }
}
